﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Public;
using System.Data;
namespace DAL
{
    public class DonVi_DAL
    {
        Connection conn = new Connection();

        #region "Lay danh sach Don Vi Cong Tac"
        public DataTable LayDSDonVi()
        {
            return conn.LoadData("Select * from DONVI");
        }
        #endregion

        #region "Them Don Vi Cong Tac"
        public int ThemDonVi(DonVi_PB dv_pb)
        {
            int nparameter = 4;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvi";   value[0] = DonVi_PB.MaDonVi;
            name[1] = "@tendonvi";  value[1] = DonVi_PB.TenDonVi;
            name[2] = "@diachi";    value[2] = DonVi_PB.DiaChi;
            name[3] = "@captochuc"; value[3] = DonVi_PB.CapToChuc;

            string sql = "insert into DONVI values (@madonvi,@tendonvi,@diachi,@captochuc)";
            return conn.Update(sql,name,value,nparameter);
        }


        public bool Exists(string ma)
        {
            string sql = string.Format("select * from DONVI where MADONVI = '{0}'", ma);
            DataTable dt = conn.LoadData(sql);
            if (dt == null) return false;

            return dt.Rows.Count > 0;
        }


        public int ThemSuaDonVi(DonVi_PB dv_pb)
        {
            int nparameter = 4;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvi"; value[0] = DonVi_PB.MaDonVi;
            name[1] = "@tendonvi"; value[1] = DonVi_PB.TenDonVi;
            name[2] = "@diachi"; value[2] = DonVi_PB.DiaChi;
            name[3] = "@captochuc"; value[3] = DonVi_PB.CapToChuc;



            string sql = @"if exists (select * from DONVI where MADONVI = @madonvi)
                            UPDATE [DONVI]
                               SET 
                                   [TENDONVI]	= @tendonvi
                                  ,[DIACHI]		= @diachi
                                  ,[CAPTOCHUC]	= @captochuc
                             WHERE  MADONVI		= @madonvi 
                            else
                            INSERT INTO [DONVI]
                                       ([MADONVI]
                                       ,[TENDONVI]
                                       ,[DIACHI]
                                       ,[CAPTOCHUC])
                                 VALUES (@madonvi,@tendonvi,@diachi,@captochuc)";


           // string sql = "insert into DONVI values (@madonvi,@tendonvi,@diachi,@captochuc)";
            return conn.Update(sql, name, value, nparameter);
        }



        #endregion

        #region "Sua Don Vi Cong Tac"
        public int SuaDonVi(DonVi_PB dv_pb)
        {
            int nparameter = 4;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvi"; value[0] = DonVi_PB.MaDonVi;
            name[1] = "@tendonvi"; value[1] = DonVi_PB.TenDonVi;
            name[2] = "@diachi"; value[2] = DonVi_PB.DiaChi;
            name[3] = "@captochuc"; value[3] = DonVi_PB.CapToChuc;

            string sql = "update DONVI set TENDONVI=@tendonvi,DIACHI=@diachi,CAPTOCHUC = @captochuc where MADONVI=@madonvi";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        #region "Xoa Don Vi Cong Tac"
        public int XoaDonVi(string madonvi)
        {
            int nparameter = 1;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@madonvi"; value[0] = madonvi;

            string sql = "delete from DONVI where MADONVI=@madonvi";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion
    }
}
